package com.mrlv.ht.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrlv.ht.mapper.ModuLeMapper;
import com.mrlv.ht.pojo.ModuLe;
import com.mrlv.ht.pojo.ModuLeExample;
import com.mrlv.ht.service.IModuLeService;

@Controller
public class ModuleController {
	
	@Resource
	private IModuLeService moduLeService;
	
	@Resource
	private ModuLeMapper modulemapp;
	
	@RequestMapping("/sysadmin/module/list")
	public String list(Model model) {
		List<ModuLe> moduLes = moduLeService.selectByExample(new ModuLeExample());
		//List<ModuLe> moduLe = moduLeService.findAll();
		//System.out.println(moduLe.size());
		model.addAttribute("moduLes", moduLes);
		//System.out.println(moduLeService.selectCount());
		return "/sysadmin/module/jModuleList";
	}
	
	@RequestMapping("/sysadmin/module/tocreate")
	public String tocreate(Model model) {
		List<ModuLe> moduLes = moduLeService.selectByExample(new ModuLeExample());
		model.addAttribute("moduLes", moduLes);
		return "/sysadmin/module/jModuleCreate";
	}
	
	@RequestMapping("/sysadmin/module/insert")
	public String insert(ModuLe module) {
		moduLeService.insertSelective(module);
		return "redirect:/sysadmin/module/list";
	}
	
	@RequestMapping("/sysadmin/module/toupdate")
	public String toupdate(Model model) {
		model.addAttribute("err", "该功能尚未完善");
		return "forward:/sysadmin/module/list";
	}
	
	@RequestMapping("/sysadmin/module/delete")
	public String delete(String[] moduleId) {
		if(moduleId != null) {
			moduLeService.deleteBatch(moduleId);
		}
		return "redirect:/sysadmin/module/list";
	}
	
	@RequestMapping("/sysadmin/module/stop")
	public String stop(String[] moduleId) {
		if(moduleId == null) {
			return "redirect:/sysadmin/module/list";
		}
		ModuLe moduLe = new ModuLe();
		if(moduleId.length == 1) {
			moduLe.setModuleId(moduleId[0]);
			moduLe.setState(new BigDecimal(0));
			moduLeService.updateByPrimaryKeySelective(moduLe);
		} else if(moduleId.length > 1) {
			moduLeService.updateBatch0(moduleId);
		}
		return "redirect:/sysadmin/module/list";
	}
	
	@RequestMapping("/sysadmin/module/start")
	public String start(String[] moduleId) {
		if(moduleId == null) {
			return "redirect:/sysadmin/module/list";
		}
		ModuLe moduLe = new ModuLe();
		if(moduleId.length == 1) {
			moduLe.setModuleId(moduleId[0]);
			moduLe.setState(new BigDecimal(1));
			moduLeService.updateByPrimaryKeySelective(moduLe);
		} else if(moduleId.length > 1) {
			moduLeService.updateBatch1(moduleId);
		}
		return "redirect:/sysadmin/module/list";
	}
}
